package com.wbcs.system.permission;

import com.wbcs.config.component.IComponentConfigBean;
import com.wbcs.system.assistant.AuthorizationAssistant;
import com.wbcs.util.Consts;

public class ComponentPermissionBean extends AbsPermissionBean
{
    protected IComponentConfigBean ccbean; 
    
    public ComponentPermissionBean(IComponentConfigBean ccbean)
    {
        this.ccbean=ccbean;
    }

    @Override
	public IComponentConfigBean getComponentConfigBean()
    {
        return ccbean;
    }

    public void authorize(String parttype,String partid,String permissiontype,String permissionvalue)
    {
        if(parttype==null||parttype.trim().equals(""))
        {
            addPermission(permissiontype,permissionvalue);
        }else
        {
            this.getChildPermissionBean(parttype,true).authorize(partid,permissiontype,permissionvalue);
        }
    }

    public int checkPermission(String parttype,String partid,String permissiontype,String permissionvalue)
    {
        if(!AuthorizationAssistant.getInstance().isExistPermissiontype(permissiontype)) return Consts.CHKPERMISSION_UNSUPPORTEDTYPE;
        if(!AuthorizationAssistant.getInstance().isExistValueOfPermissiontype(permissiontype,permissionvalue))
            return Consts.CHKPERMISSION_UNSUPPORTEDVALUE;
        
        if(parttype==null||parttype.trim().equals(""))
        {
            permissiontype=permissiontype.trim();
            permissionvalue=permissionvalue.toLowerCase().trim();
            String myPermissionvalue=this.getPermission(permissiontype);
            if(myPermissionvalue==null||myPermissionvalue.trim().equals("")) return Consts.CHKPERMISSION_EMPTY;
            if(myPermissionvalue.toLowerCase().trim().equals(permissionvalue))
            {//显式授的权限值即为要判断的permissionvalue
                return Consts.CHKPERMISSION_YES;
            }else
            {
                return Consts.CHKPERMISSION_NO;
            }
        }else
        {
            ComponentPartPermissionBean cpauthBean=this.getChildPermissionBean(parttype,false);
            if(cpauthBean==null) return Consts.CHKPERMISSION_EMPTY;
            return cpauthBean.checkPermission(partid,permissiontype,permissionvalue);
        }
    }
}

